﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace OrganizedIN.UI
{
    /// <summary>
    /// Summary description for ContactSuggest1
    /// </summary>
    public class ContactSuggest1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            OIDataContext dc = new OIDataContext();
            string tag = context.Request["tag"];
            string uname = context.Request["uid"];
            var result = (from d in dc.Contacts
                          where d.FromUName == uname && d.Approved == true && (d.UserInformation1.FirstName.ToLower().IndexOf(tag) != -1 || d.UserInformation1.LastName.ToLower().IndexOf(tag) != -1 || d.UserInformation1.username.ToLower().IndexOf(tag) != -1)
                          select new { caption = d.UserInformation1.FirstName + " " + d.UserInformation1.LastName, value = d.UserInformation1.username }).Distinct().AsEnumerable();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            var output = serializer.Serialize(result);
            context.Response.Write(output);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}